window.onload = function () {
  var oDiv = document.getElementById('div1');

  oDiv.onmousedown = function (ev) {
    var oEvent = ev || event;
    var posX = oEvent.clientX - oDiv.offsetLeft;
    var posY = oEvent.clientY - oDiv.offsetTop;

    if (oDiv.setCapture) {
      oDiv.onmousemove = mouseMove;
      oDiv.onmouseup = mouseUp;
      oDiv.setCapture();
    } else {
      document.onmousemove = mouseMove;
      document.onmouseup = mouseUp;
    }

    function mouseMove(ev) {
      var oEvent = ev || event;
      var l = oEvent.clientX - posX;
      var t = oEvent.clientY - posY;

      oDiv.style.left = l + 'px';
      oDiv.style.top = t + 'px';
    }

    function mouseUp() {
      this.onmousemove = null;
      this.onmouseup = null;
      if (this.releaseCapture) {
        this.releaseCapture();
      }
    }

    return false;
  };
};
